package com.wuzhun.wxmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.wuzhun.wxmp.mapper.WechatMapper;
import com.wuzhun.wxmp.model.dto.BaseDTO;
import com.wuzhun.wxmp.model.dto.WechatDTO;
import com.wuzhun.wxmp.model.entity.Wechat;
import com.wuzhun.wxmp.service.IWechatService;
import com.wuzhun.wxmp.util.DateTimeUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author renbaolin
 * @since 2022-12-03
 */
@Service
public class WechatServiceImpl extends ServiceImpl<WechatMapper, Wechat> implements IWechatService {

    @Resource
    private WechatMapper wechatMapper;

    @Override
    public Page<WechatDTO> listByPage(Page<Wechat> wechatPage, QueryWrapper<Wechat> queryWrapper) {
        Page<Wechat> page = wechatMapper.selectPage(wechatPage, queryWrapper);
        return covertPageWechat(page);
    }

    @Override
    public List<WechatDTO> queryCategoryById(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyList();
        }
        List<Wechat> categories = wechatMapper.selectBatchIds(idList);
        return convertWechatList(categories);
    }

    @Override
    public List<WechatDTO> queryList() {
        List<Wechat> wechatList = super.list();
        return convertWechatList(wechatList);
    }

    @Override
    public List<WechatDTO> queryListByWrapper(QueryWrapper<Wechat> queryWrapper) {
        List<Wechat> wechatList = super.list(queryWrapper);
        return convertWechatList(wechatList);
    }

    @Override
    public WechatDTO queryOne(Integer id) {
        Wechat wechat = wechatMapper.selectById(id);
        return convertWechat(wechat);
    }

    @Override
    public boolean batchDelete(List<Integer> idList) {
        return wechatMapper.deleteBatchIds(idList) > 0;
    }

    @Override
    public void fillCategoryInfo(List<? extends BaseDTO> baseDTOS) {
        List<Integer> categoryIdList = baseDTOS.stream().map(BaseDTO::getCid).collect(Collectors.toList());
        List<WechatDTO> wechatDTOList = this.queryCategoryById(categoryIdList);
        if (CollectionUtils.isNotEmpty(wechatDTOList)) {
            Map<Integer, WechatDTO> categoryMap = wechatDTOList.stream().collect(Collectors.toMap(WechatDTO::getId, Function.identity()));
            baseDTOS.forEach(softDTO -> {
                WechatDTO wechatDTO = categoryMap.get(softDTO.getCid());
                if (wechatDTO != null) {
                    softDTO.setCategoryName(wechatDTO.getName());
                }
            });
        }
    }

    /**
     * 模型转换
     *
     * @param wechatList
     * @return
     */
    private List<WechatDTO> convertWechatList(List<Wechat> wechatList) {
        List<WechatDTO> wechatDTOList = Lists.newArrayListWithCapacity(wechatList.size());
        wechatList.forEach(category -> wechatDTOList.add(convertWechat(category)));
        return wechatDTOList;
    }

    private WechatDTO convertWechat(Wechat wechat) {
        WechatDTO wechatDTO = new WechatDTO();
        BeanUtils.copyProperties(wechat, wechatDTO);
        wechatDTO.setFormatCreatedTime(DateTimeUtil.getFormatDateTime(wechat.getCreatedTime()));
        return wechatDTO;
    }

    /**
     * 转换wechat
     *
     * @param wechatPage
     * @return
     */
    private Page<WechatDTO> covertPageWechat(Page<Wechat> wechatPage) {

        Page<WechatDTO> wechatDTOPage = new Page<>();
        if (wechatPage == null || CollectionUtils.isEmpty(wechatPage.getRecords())) {
            return wechatDTOPage;
        }
        wechatDTOPage.setCurrent(wechatPage.getCurrent());
        wechatDTOPage.setTotal(wechatPage.getTotal());
        wechatDTOPage.setPages(wechatPage.getPages());
        wechatDTOPage.setSize(wechatPage.getSize());
        List<Wechat> wechatList = wechatPage.getRecords();
        List<WechatDTO> wechatDTOList = Lists.newArrayListWithCapacity(wechatList.size());
        wechatList.forEach(wechat -> wechatDTOList.add(convertWechat(wechat)));
        wechatDTOPage.setRecords(wechatDTOList);
        return wechatDTOPage;
    }
}
